﻿using ContaPlanner.Models;
using System;
using System.ComponentModel;
using System.Windows.Controls;

namespace ContaPlanner
{
    static class ExtensionMethods
    {
        public static bool IsLaboral(this DateTime value)
        {
            if (value.DayOfWeek == DayOfWeek.Saturday || value.DayOfWeek == DayOfWeek.Sunday) return false;
            var eastersunday = EasterSunday(value.Year);
            if (eastersunday.AddDays(1).Date == value.AddDays(1).Date) return false; // lunes de pascua (2e paasdag)
            if (eastersunday.AddDays(39).Date == value.AddDays(39).Date) return false; // ascensión (hemelvaartsdag)
            if (eastersunday.AddDays(49).Date == value.AddDays(50).Date) return false; // pascua granada (2e pinksterdag)
            if (value.Day == 1 && value.Month == 1) return false; // año nuevo
            if (value.Day == 27 && value.Month == 4) return false; // koningsdag
            if (value.Day == 25 && value.Month == 12) return false; // navidad
            if (value.Day == 26 && value.Month == 12) return false; // san esteban
            return true;
        }

        public static string Categoria(this TipoCuenta value)
        {
            var fi = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }

        public static TipoCuenta Tipo(this string categoria)
        {
            foreach (var field in typeof(TipoCuenta).GetFields())
            {
                var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attribute != null) if (attribute.Description == categoria) return (TipoCuenta)field.GetValue(null);
                else if (field.Name == categoria) return (TipoCuenta)field.GetValue(null);
            }
            return default(TipoCuenta);
        }

        public static TipoCuenta Tipo(this Expander expander)
        {
            var expandername = expander.Tag.ToString();
            foreach (var field in typeof(TipoCuenta).GetFields())
            {
                var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attribute != null) if (attribute.Description == expandername) return (TipoCuenta)field.GetValue(null);
                    else if (field.Name == expandername) return (TipoCuenta)field.GetValue(null);
            }
            return default(TipoCuenta);
        }

        public static string Descripcion(this TipoAsiento value)
        {
            var fi = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }

        public static DateTime EasterSunday(int anyo)
        {
            var g = anyo % 19;
            var c = anyo / 100;
            var h = (c - c / 4 - (8 * c + 13) / 25 + 19 * g + 15) % 30;
            var i = h - h / 28 * (1 - h / 28 * (29 / (h + 1)) * ((21 - g) / 11));
            var dia = i - ((anyo + anyo / 4 + i + 2 - c + c / 4) % 7) + 28;
            var mes = 3;
            if (dia <= 31) return new DateTime(anyo, mes, dia);
            mes++;
            dia -= 31;
            return new DateTime(anyo, mes, dia);
        }
    }
}
